//#pragma comment (lib, "glaux.lib")

#using <system.drawing.dll>

#include <windows.h>		// Header File For Windows
#include <gl\gl.h>			// Header File For The OpenGL32 Library
#include <gl\glu.h>			// Header File For The GLu32 Library
#include <gl\glaux.h>		// Header File For The Glaux Library

#include <stdlib.h>// for MAX_PATH
#include <iostream>
#include <fstream>
#include < vcclr.h >

using namespace System;
using namespace System::Drawing;
using namespace System::Drawing::Imaging;

class image
{
public:
	AUX_RGBImageRec *T;			// texture file
	char *f;

public:
	image(String^ Filename)
	{ getExtension(Filename); }

	~image(){ remove(f); }

	char *systochar(String^ str)
	{
		pin_ptr<const wchar_t> wch = PtrToStringChars(str);
	    size_t convertedChars = 0;
	    size_t  sizeInBytes = ((str->Length + 1) * 2);
	    errno_t err = 0;
	    char *ch = (char *)malloc(sizeInBytes);
		err = wcstombs_s(&convertedChars, ch, sizeInBytes, wch, sizeInBytes);
		if (err != 0){ std::cerr << "wcstombs_s  failed!\n"; }
		return ch;
	}

	//B = bitmap, J = jpeg, P = png, G = gif
	void getExtension(String^ path)
	{
		std::ofstream fout;
		fout.open("working.txt", std::ios::app);
		
		//find last dot, get anything after as extension
		int p = (path->LastIndexOf(".")) + 1;
		String^ ex = path->Substring(p);
		fout << "ex = " << systochar(ex) << std::endl;
		if(!ex->Equals("bmp"))
		{ 
			int pt = path->Length;
			String^ sub = path->Substring(0, pt - (pt - p));
			String^ newpath = sub + "bmp";
			fout << "orig path = " << systochar(path) << std::endl;
			fout << "new path = " << systochar(newpath) << std::endl;
			convertToBMP(path, newpath);
			f = systochar(newpath);
			LoadBMP(systochar(newpath));
		}
		else{ LoadBMP(systochar(path)); }
		fout.close();
	}

	AUX_RGBImageRec* getImage(){ return T; }

	void convertToBMP(String^ path, String^ np)
	{
		std::ofstream fout;
		fout.open("conversion.txt", std::ios::app);
		fout << "Loading: " << systochar(path) << std::endl;
		Image^ image = Image::FromFile(path);
		fout << "here";
		image->Save(np, ImageFormat::Bmp);
		//remove original
		fout << "Removing: " << systochar(path) << std::endl;
		remove(systochar(path));
		fout << "here2";
		fout.close();
	}

	/************ LOAD BITMAP **********************************/
	void LoadBMP(char *Filename)
	{
		FILE *File = NULL;

		// Make Sure A Filename Was Given
		if (!Filename){ T = NULL; }

		fopen_s(&File, Filename,"r");						// Check To See If The File Exists
		
		int len = strlen(Filename)+1;
		wchar_t *fn = new wchar_t[len];
		if(fn == 0 ) exit(1);
		memset(fn,0,len);
		MultiByteToWideChar(CP_ACP, NULL,Filename, -1, fn,len );

		if(File)
		{
			std::ofstream myfile;
			myfile.open("done2.txt", std::ios::app);
			myfile << Filename << " exists!\n";
			myfile.close();
			
			fclose(File);							// Close The Handle
			//return auxDIBImageLoad((TCHAR *)Filename);				// Load The Bitmap And Return A Pointer
			T = auxDIBImageLoad(fn);				// Load The Bitmap And Return A Pointer
		}
		else
		{
			T = NULL;								// If Load Failed Return NULL
		}
	}
};